package com.acme.bankapp.domain.bank.client;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.acme.bankapp.domain.bank.account.Account;

@SuppressWarnings("serial")
public class Client implements java.io.Serializable {
	public final static int MAX_ACCOUNT = 3;

	public int getCountAccounts() {
		return accounts.size();
	}

	private Set<Account> accounts;

	public void addAccount(Account account) throws ArrayIndexOutOfBoundsException {
		if (getCountAccounts() == MAX_ACCOUNT)
			throw new ArrayIndexOutOfBoundsException("Client already contains the maximum number of accounts");
		accounts.add(account);
	}

	private String name;

	private Gender gender;

	/**
	 *
	 * @param name name of client
	 * @param isMan is man - true, false - woman
	 */
	public Client(String name, Gender gender) {
		this.name = name;
		this.gender = gender;
		this.accounts = new HashSet<Account>();
	}

	public Set<Account> getAccounts() {
		return Collections.unmodifiableSet(accounts);
	}

	public String getClientSalution() {
		return gender.getAppeal() + " " + name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + accounts.hashCode();
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (!accounts.equals(other.accounts))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuffer text = new StringBuffer(this.getClientSalution());
		text.append(". Accounts:");
		for (Account account : accounts) {
			text.append(" " + account.toString() + ";");
		}
		return text.toString();
	}
}